package com.imedway.ihe.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
 
@Slf4j
@Configuration
@ComponentScan(basePackages="com.imedway.ihe.dtz.xds.registry.sub")
@EnableTransactionManagement//开启事务
public class JdbcTemplateConfig {
 
    @Bean
    public JdbcTemplate jdbcTemplate() {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> 配置jdbcTemplate >>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        return new JdbcTemplate(dataSource());
    }

    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(new JdbcTemplate(dataSource()));
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        transactionManager.setDefaultTimeout(30000);
        transactionManager.setRollbackOnCommitFailure(true);
        transactionManager.setGlobalRollbackOnParticipationFailure(true);
        return transactionManager;
    }
 
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("xds");
        dataSource.setPassword("xds");
        //        dataSource.setUrl("jdbc:mysql://192.168.5.61:3306/xds?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");
        dataSource.setUrl("jdbc:mysql://113.140.81.69:13306/xds?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");//外网
        return dataSource;
    }
 
}
